package com.team.RunsFast_Api.service.impl;

import com.team.RunsFast_Api.dao.CouponDao;
import com.team.RunsFast_Api.dao.CouponInfoDao;
import com.team.RunsFast_Api.dto.CouponDto;
import com.team.RunsFast_Api.service.intf.CouponInfoService;
import com.team.RunsFast_Api.service.intf.CouponService;
import com.team.runsfast.entity.CouponInfo;
import com.team.runsfast.vo.R;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class CouponServiceImpl implements CouponService {

    private final CouponDao dao;

    private final CouponInfoDao couponInfoDao;

    private final CouponInfoService couponInfoService;

    public CouponServiceImpl(CouponDao dao, CouponInfoDao couponInfoDao, CouponInfoService couponInfoService) {
        this.dao = dao;
        this.couponInfoDao = couponInfoDao;
        this.couponInfoService = couponInfoService;
    }

    @Override
    public R selectByIds2(String token) {
        List<CouponInfo> couponInfos = couponInfoService.selectByUid(token);

        List<CouponDto> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Calendar calendar1 = Calendar.getInstance();
        for (CouponInfo ci:couponInfos) {
            List<CouponDto> couponDto = dao.selectByIds2(ci.getCoupid(),ci.getId());
            System.out.println(couponDto);
            for (CouponDto couponDto1:couponDto) {
                Date starttime = couponDto1.getCtime();
                calendar.setTime(starttime);
                calendar1.setTime(new Date());
                if(calendar.get(Calendar.DATE) == calendar1.get(Calendar.DATE)){
                    list.add(couponDto1);
                }else {
                    couponInfoDao.updateCouponInfo(couponDto1.getCoupinfoid());
                }
            }
        }
        return R.ok(list);
    }

}
